﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery.DAL
{
    public class ChannelDAL
    {
        private static Channel CreateChannelFromReader(SqlDataReader reader)
        {
            Channel channel = new Channel
            {
                ChannelID = Utility.StringParse(reader[ChannelColumns.ChannelID.ToString()]),
                ChannelName = Utility.StringParse(reader[ChannelColumns.ChannelName.ToString()]),
                Number = Utility.StringParse(reader[ChannelColumns.Number.ToString()])
            };

            return channel;
        }

        public static List<Channel> GetChannelListForCMB()
        {
            try
            {
                List<Channel> channelList = new List<Channel>();

                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.ChannelGetListForCMB))
                {
                    while (reader.Read())
                    {
                        channelList.Add(CreateChannelFromReader(reader));
                    }
                }

                return channelList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetChannelListForCMB", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_CHANNEL_GET);
            }
        }

        /// <summary>
        /// channel get list by no and not load channel has result
        /// </summary>
        public static List<Channel> GetChannelListNotResult(string no, DateTime resultDate)
        {
            try
            {
                List<Channel> channelList = new List<Channel>();

                DataAccessHelper.SqlCmd.Parameters.Add("No", SqlDbType.NVarChar).Value = no;
                DataAccessHelper.SqlCmd.Parameters.Add("ResultDate", SqlDbType.Date).Value = resultDate;
                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.ChannelGetListNotResult))
                {
                    while (reader.Read())
                    {
                        channelList.Add(CreateChannelFromReader(reader));
                    }
                }

                return channelList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetChannelListNotResult", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_CHANNEL_GET);
            }
        }
    }
}